package com.ant.storage.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ant.storage.common.ResultVo;
import com.ant.storage.service.ICityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/city")
@Api(tags = "城市信息接口")
public class CityController {

    @Value("${loginUrl.url}")
    private String Url;

    @Autowired
    private ICityService cityService;

    @ApiOperation(value = "查看城市")
    @PostMapping("/queryCity")
    public ResultVo queryCity(){
        return cityService.queryCity();
    }

    @ApiOperation(value = "根据id查看部门")
    @GetMapping("/queryDept")
    public ResultVo queryDept(Integer city_id){
        return cityService.queryDept(city_id);
    }


    @ApiOperation(value = "")
    @GetMapping("/getCityList")
    public ResultVo getCityList() throws Exception {
        String returnMsg = HttpUtil.get(Url+"/moving/city/findCityList");
        JSONObject responseJsonObject = JSONUtil.parseObj(returnMsg);
        if(ObjectUtil.isNull(responseJsonObject)) {
            throw new Exception("响应异常:获取信息为空!");
        }
        String code = responseJsonObject.getStr("code");
        if(code.equals("200")){
            String tableData = responseJsonObject.getStr("data");
            return ResultVo.success(responseJsonObject.getStr("msg"), JSONUtil.parseObj(tableData));
        }
        return ResultVo.fail(responseJsonObject.getStr("msg"));
    }

}
